<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />





  <script>
  (function(i,s,o,g,r,a,m){i["DaoVoiceObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;a.charset="utf-8";m.parentNode.insertBefore(a,m)})(window,document,"script",('https:' == document.location.protocol ? 'https:' : 'http:') + "//widget.daovoice.io/widget/0f81ff2f.js","daovoice")
  daovoice('init', {
      app_id: "0b6e024b"
    });
  daovoice('update');
  </script>














  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="c," />










<meta name="keywords" content="c">
<meta property="og:type" content="article">
<meta property="og:title" content="总结的一些 [牛客] 上的算法题">
<meta property="og:url" content="http://yoursite.com/2018/08/31/c-c++/c_note_1/index.html">
<meta property="og:site_name" content="万般皆下品 唯有读书高">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/9140378-eefb30e5e7ac737f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-09-06T14:40:51.768Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="总结的一些 [牛客] 上的算法题">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/9140378-eefb30e5e7ac737f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/08/31/c-c++/c_note_1/"/>





  <title>总结的一些 [牛客] 上的算法题 | 万般皆下品 唯有读书高</title>
  








</head>


   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas>
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script>
   <script type="text/javascript" src="/js/src/fireworks.js"></script>


<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">万般皆下品 唯有读书高</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">小生的学习笔记</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            关于
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/31/c-c++/c_note_1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="HyYyr Wang">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/icon.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="万般皆下品 唯有读书高">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">总结的一些 [牛客] 上的算法题</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-31T22:13:31+08:00">
                2018-08-31
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/c-c/" itemprop="url" rel="index">
                    <span itemprop="name">c-c++</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2,308
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  11
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <hr>
<a id="more"></a>
<p><img src="https://upload-images.jianshu.io/upload_images/9140378-eefb30e5e7ac737f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="timg.jpg"></p>
<h1 id="牛客官网："><a href="#牛客官网：" class="headerlink" title="牛客官网："></a><a href="https://www.nowcoder.com" target="_blank" rel="noopener">牛客官网</a>：</h1><ul>
<li>反序输出  ~ 替换空格 ~ 句子反转</li>
<li>数字颠倒 ~ 字符串分离 ~ 明明的随机数</li>
<li>计算字符个数 ~ 字符串最后一个单词的长度</li>
<li>从尾到头打印链表 ~ 跳台阶</li>
<li>斐波那契数列 ~ 10000的阶乘</li>
</ul>
<h2 id="1-反序输出"><a href="#1-反序输出" class="headerlink" title="1. 反序输出"></a>1. 反序输出</h2><h3 id="输入任意4个字符-如：abcd-，-并按反序输出-如：dcba"><a href="#输入任意4个字符-如：abcd-，-并按反序输出-如：dcba" class="headerlink" title="输入任意4个字符(如：abcd)， 并按反序输出(如：dcba)"></a>输入任意4个字符(如：abcd)， 并按反序输出(如：dcba)</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        <span class="keyword">while</span> (scanner.hasNext())&#123;</span><br><span class="line">            StringBuilder str = <span class="keyword">new</span> StringBuilder(scanner.next());</span><br><span class="line">            str.reverse();</span><br><span class="line">            System.out.println(str.toString());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="2-替换空格"><a href="#2-替换空格" class="headerlink" title="2. 替换空格"></a>2. 替换空格</h2><h3 id="请实现一个函数，将一个字符串中的空格替换成“-20”。例如，当字符串为We-Are-Happy-则经过替换之后的字符串为We-20Are-20Happy"><a href="#请实现一个函数，将一个字符串中的空格替换成“-20”。例如，当字符串为We-Are-Happy-则经过替换之后的字符串为We-20Are-20Happy" class="headerlink" title="请实现一个函数，将一个字符串中的空格替换成“%20”。例如，当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy"></a>请实现一个函数，将一个字符串中的空格替换成“%20”。例如，当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> String <span class="title">replaceSpace</span><span class="params">(StringBuffer str)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (str == <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        StringBuilder sb = <span class="keyword">new</span> StringBuilder();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; str.length(); i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (str.charAt(i) == <span class="string">' '</span>)&#123;</span><br><span class="line">                sb.append(<span class="string">'%'</span>);</span><br><span class="line">                sb.append(<span class="string">'2'</span>);</span><br><span class="line">                sb.append(<span class="string">'0'</span>);</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                sb.append(str.charAt(i));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> sb.toString();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="3-句子反转"><a href="#3-句子反转" class="headerlink" title="3. 句子反转"></a>3. 句子反转</h2><h3 id="给定一个句子-只包含字母和空格-将句子中的单词位置反转，单词用空格分割-单词之间只有一个空格，前后没有空格比如-（1）-“hello-xiao-mi”-gt-“mi-xiao-hello”"><a href="#给定一个句子-只包含字母和空格-将句子中的单词位置反转，单词用空格分割-单词之间只有一个空格，前后没有空格比如-（1）-“hello-xiao-mi”-gt-“mi-xiao-hello”" class="headerlink" title="给定一个句子 (只包含字母和空格) 将句子中的单词位置反转，单词用空格分割, 单词之间只有一个空格，前后没有空格比如: （1） “hello xiao mi”-&gt; “mi xiao hello”"></a>给定一个句子 (只包含字母和空格) 将句子中的单词位置反转，单词用空格分割, 单词之间只有一个空格，前后没有空格比如: （1） “hello xiao mi”-&gt; “mi xiao hello”</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        String str = scanner.nextLine();</span><br><span class="line">        String string = reverse(str);</span><br><span class="line">        System.out.println(string);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">reverse</span><span class="params">(String str)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">        String[] s = str.split(<span class="string">" "</span>);</span><br><span class="line">        StringBuilder sb = <span class="keyword">new</span> StringBuilder();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = s.length - <span class="number">1</span>; i &gt; <span class="number">0</span> ; i --) &#123;</span><br><span class="line">            sb.append(s[i]);</span><br><span class="line">            sb.append(<span class="string">" "</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        sb.append(s[<span class="number">0</span>]);</span><br><span class="line">        <span class="keyword">return</span> sb.toString();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-数字颠倒"><a href="#4-数字颠倒" class="headerlink" title="4. 数字颠倒"></a>4. 数字颠倒</h2><h3 id="描述：输入一个整数，将这个整数以字符串的形式逆序输出程序不考虑负数的情况，若数字含有0，则逆序形式也含有0，如输入为100，输出为001"><a href="#描述：输入一个整数，将这个整数以字符串的形式逆序输出程序不考虑负数的情况，若数字含有0，则逆序形式也含有0，如输入为100，输出为001" class="headerlink" title="描述：输入一个整数，将这个整数以字符串的形式逆序输出程序不考虑负数的情况，若数字含有0，则逆序形式也含有0，如输入为100，输出为001"></a>描述：输入一个整数，将这个整数以字符串的形式逆序输出程序不考虑负数的情况，若数字含有0，则逆序形式也含有0，如输入为100，输出为001</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> k,i;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;k);</span><br><span class="line">    <span class="keyword">if</span>(k == <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%d"</span>,k);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(k != <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        i = k % <span class="number">10</span>;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"%d"</span>,i);</span><br><span class="line">        k = k / <span class="number">10</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="5-字符串分离"><a href="#5-字符串分离" class="headerlink" title="5. 字符串分离"></a>5. 字符串分离</h2><h3 id="连续输入字符串，请按长度为8拆分每个字符串后输出到新的字符串数组长度不是8整数倍的字符串请在后面补数字0，空字符串不处理"><a href="#连续输入字符串，请按长度为8拆分每个字符串后输出到新的字符串数组长度不是8整数倍的字符串请在后面补数字0，空字符串不处理" class="headerlink" title="连续输入字符串，请按长度为8拆分每个字符串后输出到新的字符串数组长度不是8整数倍的字符串请在后面补数字0，空字符串不处理"></a>连续输入字符串，请按长度为8拆分每个字符串后输出到新的字符串数组长度不是8整数倍的字符串请在后面补数字0，空字符串不处理</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        <span class="keyword">while</span> (scanner.hasNextLine()) &#123;</span><br><span class="line">            String str = scanner.nextLine();</span><br><span class="line">            cutout(str);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">cutout</span><span class="params">(String str)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (str.length() &gt;= <span class="number">8</span>)&#123;</span><br><span class="line">            <span class="comment">//输出截取后的数字串</span></span><br><span class="line">            System.out.println(str.substring(<span class="number">0</span>,<span class="number">8</span>));</span><br><span class="line">            <span class="comment">//将原来的数字串，从第八位截取，为下一行做准备</span></span><br><span class="line">            str = str.substring(<span class="number">8</span>);</span><br><span class="line">            cutout(str);</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span> ((str.length() &gt; <span class="number">0</span>))&#123;</span><br><span class="line">            str += <span class="string">"00000000"</span>;</span><br><span class="line">            System.out.println(str.substring(<span class="number">0</span>,<span class="number">8</span>));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="6-明明的随机数"><a href="#6-明明的随机数" class="headerlink" title="6. 明明的随机数"></a>6. 明明的随机数</h2><ul>
<li><h3 id="明明想在学校中请一些同学一起做一项问卷调查，为了实验的客观性，他先用计算机生成了N个1到1000之间的随机整数（N≤1000），对于其中重复的数字，只保留一个，把其余相同的数去掉，不同的数对应着不同的学生的学号。然后再把这些数从小到大排序，按照排好的顺序去找同学做调查。"><a href="#明明想在学校中请一些同学一起做一项问卷调查，为了实验的客观性，他先用计算机生成了N个1到1000之间的随机整数（N≤1000），对于其中重复的数字，只保留一个，把其余相同的数去掉，不同的数对应着不同的学生的学号。然后再把这些数从小到大排序，按照排好的顺序去找同学做调查。" class="headerlink" title="明明想在学校中请一些同学一起做一项问卷调查，为了实验的客观性，他先用计算机生成了N个1到1000之间的随机整数（N≤1000），对于其中重复的数字，只保留一个，把其余相同的数去掉，不同的数对应着不同的学生的学号。然后再把这些数从小到大排序，按照排好的顺序去找同学做调查。"></a>明明想在学校中请一些同学一起做一项问卷调查，为了实验的客观性，他先用计算机生成了N个1到1000之间的随机整数（N≤1000），对于其中重复的数字，只保留一个，把其余相同的数去掉，不同的数对应着不同的学生的学号。然后再把这些数从小到大排序，按照排好的顺序去找同学做调查。</h3></li>
<li><h3 id="请你协助明明完成“去重”与“排序”的工作。"><a href="#请你协助明明完成“去重”与“排序”的工作。" class="headerlink" title="请你协助明明完成“去重”与“排序”的工作。"></a>请你协助明明完成“去重”与“排序”的工作。</h3></li>
<li><h3 id="n输入随机数的个数inputArray-n-个随机整数组成的数组-Return-Value-OutputArray-输出处理后的随机整数"><a href="#n输入随机数的个数inputArray-n-个随机整数组成的数组-Return-Value-OutputArray-输出处理后的随机整数" class="headerlink" title="n输入随机数的个数inputArray n 个随机整数组成的数组 Return Value OutputArray 输出处理后的随机整数"></a>n输入随机数的个数inputArray n 个随机整数组成的数组 Return Value OutputArray 输出处理后的随机整数</h3></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"><span class="keyword">import</span> java.util.TreeSet;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        <span class="keyword">int</span> num = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span>(scanner.hasNext() &amp;&amp; num &lt; <span class="number">10</span>) &#123;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//记录下输入的数字的数量</span></span><br><span class="line">            <span class="keyword">int</span> sum = scanner.nextInt();</span><br><span class="line"></span><br><span class="line">            <span class="comment">//建立Integer的TreeSet泛型,用它的强大的功能进行排序</span></span><br><span class="line">            TreeSet&lt;Integer&gt; tree = <span class="keyword">new</span> TreeSet&lt;&gt;();</span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; sum; i++) &#123;</span><br><span class="line">                <span class="keyword">int</span> number = scanner.nextInt();</span><br><span class="line">                tree.add(number);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//再进行循环打印</span></span><br><span class="line">            <span class="keyword">for</span> (Integer set : tree) &#123;</span><br><span class="line">                System.out.println(set);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            num ++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="7-计算字符个数"><a href="#7-计算字符个数" class="headerlink" title="7. 计算字符个数"></a>7. 计算字符个数</h2><h3 id="写出一个程序，接受一个有字母和数字以及空格组成的字符串，和一个字符，然后输出输入字符串中含有该字符的个数。不区分大小写"><a href="#写出一个程序，接受一个有字母和数字以及空格组成的字符串，和一个字符，然后输出输入字符串中含有该字符的个数。不区分大小写" class="headerlink" title="写出一个程序，接受一个有字母和数字以及空格组成的字符串，和一个字符，然后输出输入字符串中含有该字符的个数。不区分大小写"></a>写出一个程序，接受一个有字母和数字以及空格组成的字符串，和一个字符，然后输出输入字符串中含有该字符的个数。不区分大小写</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">getCount</span><span class="params">(String str,<span class="keyword">char</span> c)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">if</span> (str != <span class="keyword">null</span> &amp;&amp; str.length() &gt; <span class="number">0</span>)&#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; str.length(); i++) &#123;</span><br><span class="line">                <span class="keyword">char</span> at = str.charAt(i);</span><br><span class="line">                <span class="keyword">if</span> (c == at)&#123;</span><br><span class="line">                    count ++;</span><br><span class="line">                &#125;<span class="keyword">else</span> <span class="keyword">if</span> ((at &gt;= <span class="string">'A'</span>) &amp;&amp; (at &lt;= <span class="string">'Z'</span>) &amp;&amp; (c == at + <span class="number">32</span>))&#123;</span><br><span class="line">                    count ++;</span><br><span class="line">                &#125;<span class="keyword">else</span> <span class="keyword">if</span> ((at &gt;= <span class="string">'a'</span>) &amp;&amp; (at &lt;= <span class="string">'z'</span>) &amp;&amp; (c == at - <span class="number">32</span>))&#123;</span><br><span class="line">                    count ++;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            count =  <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> count;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Scanner scr = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        String str = scr.next();</span><br><span class="line">        <span class="keyword">char</span> c = scr.next().charAt(<span class="number">0</span>);</span><br><span class="line">        System.out.println(getCount(str,c));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="8-字符串最后一个单词的长度"><a href="#8-字符串最后一个单词的长度" class="headerlink" title="8. 字符串最后一个单词的长度"></a>8. 字符串最后一个单词的长度</h2><h3 id="计算字符串最后一个单词的长度，单词以空格隔开"><a href="#计算字符串最后一个单词的长度，单词以空格隔开" class="headerlink" title="计算字符串最后一个单词的长度，单词以空格隔开"></a>计算字符串最后一个单词的长度，单词以空格隔开</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Scanner input = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        String s = <span class="string">""</span>;</span><br><span class="line">        <span class="keyword">while</span> (input.hasNextLine())&#123;</span><br><span class="line">            s = input.nextLine();</span><br><span class="line">            System.out.println(s.length() - <span class="number">1</span> - s.lastIndexOf(<span class="string">" "</span>));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="9-从尾到头打印链表"><a href="#9-从尾到头打印链表" class="headerlink" title="9. 从尾到头打印链表"></a>9. 从尾到头打印链表</h2><h3 id="输入一个链表，从尾到头打印链表每个节点的值"><a href="#输入一个链表，从尾到头打印链表每个节点的值" class="headerlink" title="输入一个链表，从尾到头打印链表每个节点的值"></a>输入一个链表，从尾到头打印链表每个节点的值</h3><ul>
<li><h4 id="方法一：链表从尾到头输出，利用递归实现，不使用库函数直接printf输出的时候用递归比较好"><a href="#方法一：链表从尾到头输出，利用递归实现，不使用库函数直接printf输出的时候用递归比较好" class="headerlink" title="方法一：链表从尾到头输出，利用递归实现，不使用库函数直接printf输出的时候用递归比较好"></a>方法一：链表从尾到头输出，利用递归实现，不使用库函数直接printf输出的时候用递归比较好</h4></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">*  struct ListNode &#123;</span></span><br><span class="line"><span class="comment">*        int val;</span></span><br><span class="line"><span class="comment">*        struct ListNode *next;</span></span><br><span class="line"><span class="comment">*        ListNode(int x) :</span></span><br><span class="line"><span class="comment">*              val(x), next(NULL) &#123;</span></span><br><span class="line"><span class="comment">*        &#125;</span></span><br><span class="line"><span class="comment">*  &#125;;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function">vector&lt;<span class="keyword">int</span>&gt; <span class="title">printListFromTailToHead</span><span class="params">(struct ListNode* head)</span> </span>&#123;</span><br><span class="line">        vector&lt;<span class="keyword">int</span>&gt; value;</span><br><span class="line">        <span class="keyword">if</span>(head != NULL)</span><br><span class="line">        &#123;</span><br><span class="line">            value.insert(value.begin(),head-&gt;val);</span><br><span class="line">            <span class="keyword">if</span>(head-&gt;next != NULL)</span><br><span class="line">            &#123;</span><br><span class="line">                vector&lt;<span class="keyword">int</span>&gt; tempVec = printListFromTailToHead(head-&gt;next);</span><br><span class="line">                <span class="keyword">if</span>(tempVec.size()&gt;<span class="number">0</span>)</span><br><span class="line">                value.insert(value.begin(),tempVec.begin(),tempVec.end());</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> value;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ul>
<li><h4 id="方法二：用库函数，每次扫描一个节点，将该结点数据存入vector中，如果该节点有下一节点，将下一节点数据直接插入vector最前面，直至遍历完，或者直接加在最后，最后调用reverse"><a href="#方法二：用库函数，每次扫描一个节点，将该结点数据存入vector中，如果该节点有下一节点，将下一节点数据直接插入vector最前面，直至遍历完，或者直接加在最后，最后调用reverse" class="headerlink" title="方法二：用库函数，每次扫描一个节点，将该结点数据存入vector中，如果该节点有下一节点，将下一节点数据直接插入vector最前面，直至遍历完，或者直接加在最后，最后调用reverse"></a>方法二：用库函数，每次扫描一个节点，将该结点数据存入vector中，如果该节点有下一节点，将下一节点数据直接插入vector最前面，直至遍历完，或者直接加在最后，最后调用reverse</h4></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">*  struct ListNode &#123;</span></span><br><span class="line"><span class="comment">*        int val;</span></span><br><span class="line"><span class="comment">*        struct ListNode *next;</span></span><br><span class="line"><span class="comment">*        ListNode(int x) :</span></span><br><span class="line"><span class="comment">*              val(x), next(NULL) &#123;</span></span><br><span class="line"><span class="comment">*        &#125;</span></span><br><span class="line"><span class="comment">*  &#125;;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function">vector&lt;<span class="keyword">int</span>&gt; <span class="title">printListFromTailToHead</span><span class="params">(struct ListNode* head)</span> </span>&#123;</span><br><span class="line">        vector&lt;<span class="keyword">int</span>&gt; value;</span><br><span class="line">        <span class="keyword">if</span>(head != NULL)</span><br><span class="line">        &#123;</span><br><span class="line">            value.insert(value.begin(),head-&gt;val);</span><br><span class="line">            <span class="keyword">while</span>(head-&gt;next != NULL)</span><br><span class="line">            &#123;</span><br><span class="line">                value.insert(value.begin(),head-&gt;next-&gt;val);</span><br><span class="line">                head = head-&gt;next;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> value;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ul>
<li><h4 id="方法三：java递归-简洁版本"><a href="#方法三：java递归-简洁版本" class="headerlink" title="方法三：java递归 简洁版本"></a>方法三：java递归 简洁版本</h4></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    ArrayList&lt;Integer&gt; arrayList=<span class="keyword">new</span> ArrayList&lt;Integer&gt;();</span><br><span class="line">    <span class="function"><span class="keyword">public</span> ArrayList&lt;Integer&gt; <span class="title">printListFromTailToHead</span><span class="params">(ListNode listNode)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(listNode!=<span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">this</span>.printListFromTailToHead(listNode.next);</span><br><span class="line">            arrayList.add(listNode.val);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> arrayList;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="10-跳台阶"><a href="#10-跳台阶" class="headerlink" title="10. 跳台阶"></a>10. 跳台阶</h2><h3 id="一只青蛙一次可以跳上1级台阶，也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法"><a href="#一只青蛙一次可以跳上1级台阶，也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法" class="headerlink" title="一只青蛙一次可以跳上1级台阶，也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法"></a>一只青蛙一次可以跳上1级台阶，也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        System.out.println(JumpFloor(scanner.nextByte()));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">JumpFloor</span><span class="params">(<span class="keyword">int</span> target)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (target == <span class="number">1</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">if</span> (target == <span class="number">2</span>) <span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">if</span> (target == <span class="number">3</span>) <span class="keyword">return</span> <span class="number">3</span>;</span><br><span class="line">        <span class="keyword">int</span> one = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span> two = <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">int</span> result = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i &lt; target; i++) &#123;</span><br><span class="line">            result = one + two;</span><br><span class="line">            one = two;</span><br><span class="line">            two = result;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="11-斐波那契数列"><a href="#11-斐波那契数列" class="headerlink" title="11. 斐波那契数列"></a>11. 斐波那契数列</h2><h3 id="大家都知道斐波那契数列，现在要求输入一个整数n，请你输出斐波那契数列的第n项。-n-lt-39"><a href="#大家都知道斐波那契数列，现在要求输入一个整数n，请你输出斐波那契数列的第n项。-n-lt-39" class="headerlink" title="大家都知道斐波那契数列，现在要求输入一个整数n，请你输出斐波那契数列的第n项。 n &lt;= 39"></a>大家都知道斐波那契数列，现在要求输入一个整数n，请你输出斐波那契数列的第n项。 n &lt;= 39</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"><span class="keyword">import</span> com.sun.java_cup.internal.runtime.Symbol;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main12</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        System.out.println(fibinalie(scanner.nextInt()));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">fibinalie</span><span class="params">(<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> a = <span class="number">1</span>,b = <span class="number">1</span>,c = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">if</span> (n &lt; <span class="number">0</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(n == <span class="number">1</span> || n == <span class="number">2</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">3</span>; i &lt;= n ; i++) &#123;</span><br><span class="line">                c = a + b;</span><br><span class="line">                b = a;</span><br><span class="line">                a = c;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> c;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="12-10000-的阶乘"><a href="#12-10000-的阶乘" class="headerlink" title="12. 10000 的阶乘"></a>12. 10000 的阶乘</h2><ul>
<li><h3 id="还记得这是大一学C语言时候，井超老师给我们出的题，当时C语言学的还不咋扎实，所以想了有一段时间，就被搁置了"><a href="#还记得这是大一学C语言时候，井超老师给我们出的题，当时C语言学的还不咋扎实，所以想了有一段时间，就被搁置了" class="headerlink" title="还记得这是大一学C语言时候，井超老师给我们出的题，当时C语言学的还不咋扎实，所以想了有一段时间，就被搁置了"></a>还记得这是大一学C语言时候，井超老师给我们出的题，当时C语言学的还不咋扎实，所以想了有一段时间，就被搁置了</h3></li>
<li><h3 id="直到看到Black-Bao的一篇博客：求10000的阶乘-我才想起来-今天仔细琢磨了琢磨，知道了是咋个一回事"><a href="#直到看到Black-Bao的一篇博客：求10000的阶乘-我才想起来-今天仔细琢磨了琢磨，知道了是咋个一回事" class="headerlink" title="直到看到Black Bao的一篇博客：求10000的阶乘 我才想起来,今天仔细琢磨了琢磨，知道了是咋个一回事"></a>直到看到<a href="http://barackbao.com/" target="_blank" rel="noopener">Black Bao</a>的一篇博客：<a href="http://barackbao.com/2018/03/11/%E6%B1%8210000%E7%9A%84%E9%98%B6%E4%B9%98/" target="_blank" rel="noopener">求10000的阶乘</a> 我才想起来,今天仔细琢磨了琢磨，知道了是咋个一回事</h3></li>
<li><h3 id="利用进位，将阶乘的数据倒着存放于数组当中，涉及到进位的一些让人‘头大’的事情"><a href="#利用进位，将阶乘的数据倒着存放于数组当中，涉及到进位的一些让人‘头大’的事情" class="headerlink" title="利用进位，将阶乘的数据倒着存放于数组当中，涉及到进位的一些让人‘头大’的事情"></a>利用进位，将阶乘的数据倒着存放于数组当中，涉及到进位的一些让人‘头大’的事情</h3></li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="keyword">int</span> result[<span class="number">40000</span>]; <span class="comment">//保存结算结果的数组</span></span><br><span class="line">  <span class="keyword">int</span> height = <span class="number">1</span>;  <span class="comment">//结果的最高位</span></span><br><span class="line">  <span class="keyword">int</span> num;  <span class="comment">//计算阶乘的数字</span></span><br><span class="line">  <span class="built_in">cout</span> &lt;&lt; <span class="string">"请输入阶乘数："</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">  <span class="built_in">cin</span> &gt;&gt; num;</span><br><span class="line">  result[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>;i &lt;= num;i ++)</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="keyword">int</span> res = <span class="number">0</span>; <span class="comment">//进位</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>;j &lt; height;j ++)</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="keyword">int</span> buf = result[j] * i + res; <span class="comment">//计算结果</span></span><br><span class="line">      result[j] = buf % <span class="number">10</span>;  <span class="comment">//取当前位</span></span><br><span class="line">      res = buf / <span class="number">10</span>;   <span class="comment">//计算进位</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//判断是否进位</span></span><br><span class="line">    <span class="keyword">while</span> (res)</span><br><span class="line">    &#123;</span><br><span class="line">      result[height ++] = res % <span class="number">10</span>; <span class="comment">//取当前位</span></span><br><span class="line">      res /= <span class="number">10</span>;   <span class="comment">//计算进位</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">int</span> k = height - <span class="number">1</span>;k &gt;= <span class="number">0</span>;k --)</span><br><span class="line">  &#123;</span><br><span class="line">     <span class="built_in">cout</span> &lt;&lt; result[k];  <span class="comment">//数组你想输出</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">  <span class="built_in">cout</span> &lt;&lt; <span class="string">"length = "</span> &lt;&lt; height &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="13-数字转化二进制比较有所不同"><a href="#13-数字转化二进制比较有所不同" class="headerlink" title="13. 数字转化二进制比较有所不同"></a>13. 数字转化二进制比较有所不同</h2><h3 id="正好在这里复习一下-amp-、-跟"><a href="#正好在这里复习一下-amp-、-跟" class="headerlink" title="正好在这里复习一下&amp; 、^ 跟 |"></a>正好在这里复习一下&amp; 、^ 跟 |</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">  <span class="number">101010</span>        <span class="number">101010</span>        <span class="number">101010</span></span><br><span class="line">&amp; <span class="number">011100</span>      | <span class="number">011100</span>      ^ <span class="number">011100</span></span><br><span class="line">----------  -------------  ------------</span><br><span class="line">  <span class="number">001000</span>        <span class="number">111110</span>        <span class="number">110110</span>        ;</span><br></pre></td></tr></table></figure>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> num1,num2 = <span class="number">0</span>;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">"num1 = %d\nnum2 = %d"</span>,&amp;num1,&amp;num2);</span><br><span class="line">    <span class="keyword">char</span> str1[<span class="number">30</span>],str2[<span class="number">30</span>];</span><br><span class="line">    itoa(num1,str1,<span class="number">2</span>);</span><br><span class="line">    itoa(num2,str2,<span class="number">2</span>);</span><br><span class="line">    <span class="keyword">int</span> ans = num1 ^ num2;</span><br><span class="line">    <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span>(ans != <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        ans &amp;= (ans - <span class="number">1</span>);</span><br><span class="line">        count ++;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"%d"</span>,count);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/c/" rel="tag"><i class="fa fa-tag"></i> c</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/08/31/java/java-note-2/" rel="next" title="Java第一行代码 学习笔记(二) Java 异常处理机制">
                <i class="fa fa-chevron-left"></i> Java第一行代码 学习笔记(二) Java 异常处理机制
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/09/02/python/python_note_1/" rel="prev" title="Python编程快速上手 学习笔记(一) 匹配模式与正则表达式">
                Python编程快速上手 学习笔记(一) 匹配模式与正则表达式 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zOTQxMi8xNTkzOQ=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/uploads/icon.jpg"
                alt="HyYyr Wang" />
            
              <p class="site-author-name" itemprop="name">HyYyr Wang</p>
              <p class="site-description motion-element" itemprop="description">我不是程序员，也不是设计师，只是喜欢编程，热爱python，碰巧有一些想法跟一台电脑</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">24</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/wanghao15536870732" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:hyyyrwang@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.douban.com/people/183791128" target="_blank" title="豆瓣">
                      
                        <i class="fa fa-fw fa-globe"></i>豆瓣</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/be-forever-82/activities" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-globe"></i>知乎</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/964bc0f1e88e" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-globe"></i>简书</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/qq_41005535" target="_blank" title="CSND">
                      
                        <i class="fa fa-fw fa-globe"></i>CSND</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                推荐阅读
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="npm install hexo-wordcount --save" title="秋冬夜漫长" target="_blank">秋冬夜漫长</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://angus-liu.github.io" title="Angus-Liu" target="_blank">Angus-Liu</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://innofang.github.io" title="Inno Fang" target="_blank">Inno Fang</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://barackbao.com" title="Barack Bao" target="_blank">Barack Bao</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://wmpscc.github.io" title="wmpscc" target="_blank">wmpscc</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://kentanvictor.github.io" title="JohnnyTan" target="_blank">JohnnyTan</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://xmmmmmovo.github.io/" title="xmmmmmovo Zha" target="_blank">xmmmmmovo Zha</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#牛客官网："><span class="nav-number">1.</span> <span class="nav-text">牛客官网：</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-反序输出"><span class="nav-number">1.1.</span> <span class="nav-text">1. 反序输出</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#输入任意4个字符-如：abcd-，-并按反序输出-如：dcba"><span class="nav-number">1.1.1.</span> <span class="nav-text">输入任意4个字符(如：abcd)， 并按反序输出(如：dcba)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-替换空格"><span class="nav-number">1.2.</span> <span class="nav-text">2. 替换空格</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#请实现一个函数，将一个字符串中的空格替换成“-20”。例如，当字符串为We-Are-Happy-则经过替换之后的字符串为We-20Are-20Happy"><span class="nav-number">1.2.1.</span> <span class="nav-text">请实现一个函数，将一个字符串中的空格替换成“%20”。例如，当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-句子反转"><span class="nav-number">1.3.</span> <span class="nav-text">3. 句子反转</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#给定一个句子-只包含字母和空格-将句子中的单词位置反转，单词用空格分割-单词之间只有一个空格，前后没有空格比如-（1）-“hello-xiao-mi”-gt-“mi-xiao-hello”"><span class="nav-number">1.3.1.</span> <span class="nav-text">给定一个句子 (只包含字母和空格) 将句子中的单词位置反转，单词用空格分割, 单词之间只有一个空格，前后没有空格比如: （1） “hello xiao mi”-&gt; “mi xiao hello”</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-数字颠倒"><span class="nav-number">1.4.</span> <span class="nav-text">4. 数字颠倒</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#描述：输入一个整数，将这个整数以字符串的形式逆序输出程序不考虑负数的情况，若数字含有0，则逆序形式也含有0，如输入为100，输出为001"><span class="nav-number">1.4.1.</span> <span class="nav-text">描述：输入一个整数，将这个整数以字符串的形式逆序输出程序不考虑负数的情况，若数字含有0，则逆序形式也含有0，如输入为100，输出为001</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-字符串分离"><span class="nav-number">1.5.</span> <span class="nav-text">5. 字符串分离</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#连续输入字符串，请按长度为8拆分每个字符串后输出到新的字符串数组长度不是8整数倍的字符串请在后面补数字0，空字符串不处理"><span class="nav-number">1.5.1.</span> <span class="nav-text">连续输入字符串，请按长度为8拆分每个字符串后输出到新的字符串数组长度不是8整数倍的字符串请在后面补数字0，空字符串不处理</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-明明的随机数"><span class="nav-number">1.6.</span> <span class="nav-text">6. 明明的随机数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#明明想在学校中请一些同学一起做一项问卷调查，为了实验的客观性，他先用计算机生成了N个1到1000之间的随机整数（N≤1000），对于其中重复的数字，只保留一个，把其余相同的数去掉，不同的数对应着不同的学生的学号。然后再把这些数从小到大排序，按照排好的顺序去找同学做调查。"><span class="nav-number">1.6.1.</span> <span class="nav-text">明明想在学校中请一些同学一起做一项问卷调查，为了实验的客观性，他先用计算机生成了N个1到1000之间的随机整数（N≤1000），对于其中重复的数字，只保留一个，把其余相同的数去掉，不同的数对应着不同的学生的学号。然后再把这些数从小到大排序，按照排好的顺序去找同学做调查。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#请你协助明明完成“去重”与“排序”的工作。"><span class="nav-number">1.6.2.</span> <span class="nav-text">请你协助明明完成“去重”与“排序”的工作。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#n输入随机数的个数inputArray-n-个随机整数组成的数组-Return-Value-OutputArray-输出处理后的随机整数"><span class="nav-number">1.6.3.</span> <span class="nav-text">n输入随机数的个数inputArray n 个随机整数组成的数组 Return Value OutputArray 输出处理后的随机整数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-计算字符个数"><span class="nav-number">1.7.</span> <span class="nav-text">7. 计算字符个数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#写出一个程序，接受一个有字母和数字以及空格组成的字符串，和一个字符，然后输出输入字符串中含有该字符的个数。不区分大小写"><span class="nav-number">1.7.1.</span> <span class="nav-text">写出一个程序，接受一个有字母和数字以及空格组成的字符串，和一个字符，然后输出输入字符串中含有该字符的个数。不区分大小写</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-字符串最后一个单词的长度"><span class="nav-number">1.8.</span> <span class="nav-text">8. 字符串最后一个单词的长度</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#计算字符串最后一个单词的长度，单词以空格隔开"><span class="nav-number">1.8.1.</span> <span class="nav-text">计算字符串最后一个单词的长度，单词以空格隔开</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-从尾到头打印链表"><span class="nav-number">1.9.</span> <span class="nav-text">9. 从尾到头打印链表</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#输入一个链表，从尾到头打印链表每个节点的值"><span class="nav-number">1.9.1.</span> <span class="nav-text">输入一个链表，从尾到头打印链表每个节点的值</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#方法一：链表从尾到头输出，利用递归实现，不使用库函数直接printf输出的时候用递归比较好"><span class="nav-number">1.9.1.1.</span> <span class="nav-text">方法一：链表从尾到头输出，利用递归实现，不使用库函数直接printf输出的时候用递归比较好</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#方法二：用库函数，每次扫描一个节点，将该结点数据存入vector中，如果该节点有下一节点，将下一节点数据直接插入vector最前面，直至遍历完，或者直接加在最后，最后调用reverse"><span class="nav-number">1.9.1.2.</span> <span class="nav-text">方法二：用库函数，每次扫描一个节点，将该结点数据存入vector中，如果该节点有下一节点，将下一节点数据直接插入vector最前面，直至遍历完，或者直接加在最后，最后调用reverse</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#方法三：java递归-简洁版本"><span class="nav-number">1.9.1.3.</span> <span class="nav-text">方法三：java递归 简洁版本</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-跳台阶"><span class="nav-number">1.10.</span> <span class="nav-text">10. 跳台阶</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#一只青蛙一次可以跳上1级台阶，也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法"><span class="nav-number">1.10.1.</span> <span class="nav-text">一只青蛙一次可以跳上1级台阶，也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-斐波那契数列"><span class="nav-number">1.11.</span> <span class="nav-text">11. 斐波那契数列</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#大家都知道斐波那契数列，现在要求输入一个整数n，请你输出斐波那契数列的第n项。-n-lt-39"><span class="nav-number">1.11.1.</span> <span class="nav-text">大家都知道斐波那契数列，现在要求输入一个整数n，请你输出斐波那契数列的第n项。 n &lt;= 39</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-10000-的阶乘"><span class="nav-number">1.12.</span> <span class="nav-text">12. 10000 的阶乘</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#还记得这是大一学C语言时候，井超老师给我们出的题，当时C语言学的还不咋扎实，所以想了有一段时间，就被搁置了"><span class="nav-number">1.12.1.</span> <span class="nav-text">还记得这是大一学C语言时候，井超老师给我们出的题，当时C语言学的还不咋扎实，所以想了有一段时间，就被搁置了</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#直到看到Black-Bao的一篇博客：求10000的阶乘-我才想起来-今天仔细琢磨了琢磨，知道了是咋个一回事"><span class="nav-number">1.12.2.</span> <span class="nav-text">直到看到Black Bao的一篇博客：求10000的阶乘 我才想起来,今天仔细琢磨了琢磨，知道了是咋个一回事</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#利用进位，将阶乘的数据倒着存放于数组当中，涉及到进位的一些让人‘头大’的事情"><span class="nav-number">1.12.3.</span> <span class="nav-text">利用进位，将阶乘的数据倒着存放于数组当中，涉及到进位的一些让人‘头大’的事情</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-数字转化二进制比较有所不同"><span class="nav-number">1.13.</span> <span class="nav-text">13. 数字转化二进制比较有所不同</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#正好在这里复习一下-amp-、-跟"><span class="nav-number">1.13.1.</span> <span class="nav-text">正好在这里复习一下&amp; 、^ 跟 |</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">HyYyr Wang</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>


<div>
<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span id="busuanzi_container_site_pv" style='display:none'>
    本站总访问量 <span id="busuanzi_value_site_pv"></span> 次
    <span class="post-meta-divider">|</span> </span>
<span id="busuanzi_container_site_uv" style='display:none'>
    有 <span id="busuanzi_value_site_uv"></span> 人看过我的博客啦
</span>
</div>





<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共33.8k字</span>
</div>

        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
